package controllers.manage.user;

import controllers.BaseController;
import controllers.auth.Secure;
import helper.GlobalConfig;
import models.merchant.Merchant;
import models.venue.curriculum.WebUserCurriculum;
import models.weixin.WebUser;
import models.weixin.amount.WebUserAccount;
import models.weixin.amount.WebUserAccountHistory;
import org.apache.commons.lang.StringUtils;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hanbin on 17/5/10.
 */
@With(BaseController.class)
public class WebUserCtrl extends Controller {

    public static void index(Integer pageNumber, String nickName, String phone){

        Merchant merchant = Secure.getLoginUser().merchant;

        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> searchMap = new HashMap<>();

        if(StringUtils.isNotBlank(nickName)){
            searchMap.put("nickName", "%"+nickName+"%");
        }
        if(merchant != null){
            searchMap.put("merchantId", merchant.id);
        }

        if(StringUtils.isNotBlank(phone)){
            searchMap.put("phone", phone);
        }

        flash.put("menuId", "4");

        JPAExtPaginator<WebUser> resultPage = WebUser.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber, nickName, phone);

    }

    /**
     * 查看某个会员的充值纪录
     * @param id
     */
    public static void record(Long id) {
        WebUser webUser = WebUser.findById(id);
        List<WebUserAccount> webUserAccountList = WebUserAccount.findAllByWebUser(webUser);
        List<WebUserAccountHistory> historyList = WebUserAccountHistory.findAllByWebUser(webUser);
        render(webUser , webUserAccountList , historyList);
    }

    /**
     * 查看会员的课程购买记录
     * @param id
     */
    public static void curriculumOrder(long id ){
        WebUser webUser = new WebUser();
        List<WebUserCurriculum> webUserCurriculumList = new ArrayList<>();
        if(id > 0){
            webUser = WebUser.findByUserId(id);
            webUserCurriculumList = WebUserCurriculum.loadByUserId(id);
        }
        render(id ,webUser , webUserCurriculumList );
    }

    /**
     * 课程数量减少一次或者增加一次
     * type:  0 代表减少一次;  1代表增加一次
     * @param id
     * @param type
     * @param webUserId
     */
    public static void minusCurriculumNum(long id ,int  type, long webUserId ){
        WebUserCurriculum webUserCurriculum = WebUserCurriculum.findByWebUserCurriculumId(id);
        if(webUserCurriculum == null){
            flash.put("msg","课程数据不存在,可能被删除!");
            redirect("/BS0008/admin/curriculumOrder/" + webUserId );
        }

        if (type == 0 && (webUserCurriculum.beUsedNum >= webUserCurriculum.quantity || webUserCurriculum.residueNum <= 0)) {
            flash.put("msg","可用课程数量为0 ,操作失败!");
            redirect("/BS0008/admin/curriculumOrder/" + webUserId );

        }

        if (type == 1 && (webUserCurriculum.residueNum == webUserCurriculum.quantity || webUserCurriculum.beUsedNum == 0)) {
            flash.put("msg","可用课程数量已是总课程数量,不可增加!");
            redirect("/BS0008/admin/curriculumOrder/" + webUserId );

        }

        String msg = "";
        if (type == 0) {
            webUserCurriculum.beUsedNum = webUserCurriculum.beUsedNum + 1;
            webUserCurriculum.residueNum = webUserCurriculum.quantity - webUserCurriculum.beUsedNum;
            msg = "课程已成功减少一次";
        } else {
            webUserCurriculum.residueNum = webUserCurriculum.residueNum + 1;
            webUserCurriculum.beUsedNum = webUserCurriculum.beUsedNum - 1;
            msg = "课程已成功增加一次";
        }


        Secure.setBaseInfo(webUserCurriculum);
        webUserCurriculum.save();

        flash.put("msg",msg);
        redirect("/BS0008/admin/curriculumOrder/" + webUserId );

    }


}
